package com.fun_ordering.service.impl;

import com.fun_ordering.components.CodeComponents;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.mapper.*;
import com.fun_ordering.pojo.FoOrder;
import com.fun_ordering.pojo.FoOrderExample;
import com.fun_ordering.pojo.FoStatusExample;
import com.fun_ordering.service.FoOrderService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 订单表服务实现层
 *
 * @author XiaoPi
 */
@Service
public class FoOrderServiceImpl implements FoOrderService {

    @Autowired
    //订单表
    private FoOrderMapper foOrderMapper;
    @Autowired
    //菜品价格表	//订单菜品详细表
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private FoStatusMapper foStatusMapper;
    @Autowired
    private FoLossMapper foLossMapper;
    @Autowired
    private MaterialStockMapper materialStockMapper;


    /**
     * 查询全部
     * 小程序查订单不同状态
     */
    @Override
    public List<FoOrder> findAll(FoOrder foOrder) {
        FoOrderExample example = new FoOrderExample();
        FoOrderExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo(CodeComponents.DELETE_NO);
        if (!ObjectUtils.isEmpty(foOrder)) {
            if (!ObjectUtils.isEmpty(foOrder.getOrderStatus())) {
                criteria.andOrderStatusEqualTo(foOrder.getOrderStatus());
            }
            if (!ObjectUtils.isEmpty(foOrder)) {
                criteria.andPayStatusEqualTo(foOrder.getPayStatus());
            }
        }
        return foOrderMapper.selectByExample(example);
    }

    /**
     * 按分页查询
     */
    @Override
    public PageResult findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<FoOrder> page = (Page<FoOrder>) foOrderMapper.selectByExample(null);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 增加
     */
    @Override
    public Integer add(FoOrder foOrder) {
        return foOrderMapper.insert(foOrder);

    }


    /**
     * 修改
     */
    @Override
    public void update(FoOrder foOrder) {
        foOrderMapper.updateByPrimaryKey(foOrder);
    }

    /**
     * 根据ID获取实体
     *
     * @param orderId
     * @return
     */
    @Override
    public FoOrder findOne(Integer orderId) {
        //先通过id查询订单信息
        FoOrder foOrder = foOrderMapper.selectByPrimaryKey(orderId);
        //将原料表信息传入原料表的数组
        foOrder.setOrderDetailList(orderDetailMapper.findAllByOrderId(orderId));
        //创建状态扩展类
        FoStatusExample example = new FoStatusExample();
        //调用其方法，将需要的属性补充到foOrder
        FoStatusExample.Criteria criteria = example.createCriteria();
        criteria.andOrderStatusEqualTo(foOrder.getOrderStatus());
        //将状态类型的属性传入，前端打点调用
        foOrder.setFoStatus(foStatusMapper.selectByExample(example).get(0));
        return foOrder;
    }

    /**
     * 批量删除
     */
    @Override
    public void delete(Integer[] orderIds) {
       try {
           //真删
//        for (Integer orderId : orderIds) {
//            foOrderMapper.deleteByPrimaryKey(orderId);
//        }
           //逻辑删除
           for (Integer orderId : orderIds) {
               FoOrder foOrder = foOrderMapper.selectByPrimaryKey(orderId);
               foOrder.setIsDelete(CodeComponents.DELETE_OK);
               foOrderMapper.updateByPrimaryKey(foOrder);
               orderDetailMapper.updateIsDeleteByOrderId(foOrder.getOrderId());
           }
       }catch (Exception e){
           throw new RuntimeException(e);
       }

    }

    /**
     * 作用：多条件分页查询
     * 方法名：
     * 参数: [foOrder, pageNum, pageSize]
     * 返回值: com.fun_ordering.components.PageResult
     */
    @Override
    public PageResult findPage(FoOrder foOrder, int pageNum, int pageSize) {

        //分页
        PageHelper.startPage(pageNum, pageSize);
        FoOrderExample foOrderExample = new FoOrderExample();
        FoOrderExample.Criteria criteria = foOrderExample.createCriteria();
        criteria.andIsDeleteEqualTo(CodeComponents.DELETE_NO);
        foOrderExample.setOrderByClause("start_time DESC");
        if (foOrder != null) {
//				订单状态
            if (foOrder.getOrderStatus() != null && foOrder.getOrderStatus() != -1 ) {
//                System.out.println(foOrder.getOrderStatus());
                criteria.andOrderStatusEqualTo(foOrder.getOrderStatus());
            }
//				支付状态
            if (foOrder.getPayStatus() != null &&foOrder.getPayStatus() != -1) {
                criteria.andPayStatusEqualTo(foOrder.getPayStatus());
            }
//				餐桌id
            if (foOrder.getTableId() != null) {
                criteria.andTableIdEqualTo(foOrder.getTableId());
            }
            //顾客id
            if(foOrder.getCustomerId()!=null){
                criteria.andCustomerIdEqualTo(foOrder.getCustomerId());
            }
        }

        List<FoOrder> foOrderList = foOrderMapper.selectByExample(foOrderExample);
        for (FoOrder order : foOrderList) {
//            System.out.println(order);
            order.setOrderDetailList(orderDetailMapper.findAllByOrderId(order.getOrderId()));
            //获取订单状态id
            FoStatusExample example = new FoStatusExample();
            FoStatusExample.Criteria criteria1 = example.createCriteria();
            criteria1.andOrderStatusEqualTo(order.getOrderStatus());
            order.setFoStatus(foStatusMapper.selectByExample(example).get(0));
        }

        PageInfo<FoOrder> pageInfo = new PageInfo<>(foOrderList);
//        System.out.println(pageInfo);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 作用：确认出餐
     * 方法名：
     * 参数: [orderId]
     * 返回值: void
     */
    @Override
    public void orderAleardy(Integer orderId) {
        foOrderMapper.orderAlready(orderId);
    }

    @Override
    public void payAlready(Integer outTradeNo) {
        foOrderMapper.payAlready(outTradeNo);
    }

    @Override
    public void refundAlready(Integer outTradeNo) {
        foOrderMapper.refundAlready(outTradeNo);
    }
}
